import axios from "axios";
import { BASE_URL } from "./url";
import { getToken, removeToken } from "./auth";
const API = axios.create({
    baseURL: BASE_URL
})

API.interceptors.request.use(config=>{
    const {url} = config
    if(url.startsWith('/user') && !url.startsWith('/user/login') && !url.startsWith('/user/registered')){
        // 添加请求头
        config.headers.Authorization = getToken()
    }
    return config
})

API.interceptors.response.use(response=>{
    const {status} = response
    if(status===400){
        // 此时说明token失效，直接移除token即可
        removeToken()
    }

    return response
})

export { API }